package com.ruoyi.edh.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.edh.domain.EdhCoupon;
import com.ruoyi.edh.domain.vo.EdhCouponVo;
import com.ruoyi.edh.mapper.EdhCouponMapper;
import com.ruoyi.edh.service.IEdhCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
 * 易订货-优惠券Service业务层处理
 * 
 * @author xxx
 * @date 2020-09-02
 */
@Service
public class EdhCouponServiceImpl implements IEdhCouponService
{
    @Autowired
    private EdhCouponMapper edhCouponMapper;

    /**
     * 查询易订货-优惠券
     * 
     * @param id 易订货-优惠券ID
     * @return 易订货-优惠券
     */
    @Override
    public EdhCoupon selectEdhCouponById(Long id)
    {
        return edhCouponMapper.selectEdhCouponById(id);
    }

    /**
     * 查询易订货-优惠券列表
     * 
     * @param edhCoupon 易订货-优惠券
     * @return 易订货-优惠券
     */
    @Override
    public List<EdhCoupon> selectEdhCouponList(EdhCoupon edhCoupon)
    {
        return edhCouponMapper.selectEdhCouponList(edhCoupon);
    }
     /**
     * 查询易订货-优惠券
     *
     * @param id 易订货-优惠券ID
     * @return 易订货-优惠券
     */
    @Override
    public EdhCouponVo selectEdhCouponVoById(Long id)
    {
        return edhCouponMapper.selectEdhCouponVoById(id);
    }

    /**
     * 查询易订货-优惠券列表
     *
     * @param edhCoupon 易订货-优惠券
     * @return 易订货-优惠券
     */
    @Override
    public List<EdhCouponVo> selectEdhCouponVoList(EdhCoupon edhCoupon)
    {
        return edhCouponMapper.selectEdhCouponVoList(edhCoupon);
    }

    /**
     * 可被领取的优惠券列表
     */
    @Override
    public List<EdhCouponVo> availableCoupon(Long goodsId) {
        Long userId = SecurityUtils.getUserId();
        return edhCouponMapper.availableCoupon(goodsId,userId);
    }

    /**
     * 新增易订货-优惠券
     * 
     * @param edhCoupon 易订货-优惠券
     * @return 结果
     */
    @Override
    public int insertEdhCoupon(EdhCoupon edhCoupon)
    {
        Date now =  DateUtils.getNowDate();
        if (now.before(edhCoupon.getSendStartTime())){
            edhCoupon.setStatus(1);
        }
        edhCoupon.setCreateTime(now);
        return edhCouponMapper.insertEdhCoupon(edhCoupon);
    }

    /**
     * 修改易订货-优惠券
     * 
     * @param edhCoupon 易订货-优惠券
     * @return 结果
     */
    @Override
    public int updateEdhCoupon(EdhCoupon edhCoupon)
    {
        edhCoupon.setUpdateTime(DateUtils.getNowDate());
        return edhCouponMapper.updateEdhCoupon(edhCoupon);
    }

    /**
     * 批量删除易订货-优惠券
     * 
     * @param ids 需要删除的易订货-优惠券ID
     * @return 结果
     */
    @Override
    public int deleteEdhCouponByIds(Long[] ids)
    {
        return edhCouponMapper.deleteEdhCouponByIds(ids);
    }

    /**
     * 删除易订货-优惠券信息
     * 
     * @param id 易订货-优惠券ID
     * @return 结果
     */
    @Override
    public int deleteEdhCouponById(Long id)
    {
        return edhCouponMapper.deleteEdhCouponById(id);
    }
    /**
     *
     *
     * @description: 查询全部未删除的优惠卷
     * @param
     * @return: List<EdhCoupon>
     * @author: 梁东宸
     * @time: 2020/9/18 2:52 下午
     */
    @Override
    public List<EdhCoupon> selectExpiredCoupon() {
        return edhCouponMapper.selectExpiredCoupon();
    }
    /**
     *
     *
     * @description: 根据id修改已生效或已失效的优惠卷
     * @param status,id 优惠卷状态值和id
     * @return:
     * @author: 梁东宸
     * @time: 2020/9/18 2:53 下午
     */
    @Override
    @Async
    public Integer updateExpiredCoupon(String status, String id) {
        return edhCouponMapper.updateExpiredCoupon(status,id);
    }

}
